
# Major contributions to this PKGBUILD from:
# Keshav Amburay <(the ddoott ridikulus ddoott rat) (aatt) (gemmaeiil) (ddoott) (ccoomm)>
# https://aur.archlinux.org/packages/grub-git

pkgname=grub
pkgdesc="GNU GRand Unified Bootloader"
pkgver=2.02
pkgrel=3
_commit=f826330683675f0deb55b58fd229afd7d65fb053
url="https://www.gnu.org/software/grub/"
arch=('x86_64')
license=('GPL3')
backup=('boot/grub/grub.cfg' 'etc/default/grub' 'etc/grub.d/40_custom')
install=grub.install
options=('!makeflags')
conflicts=('grub-common' 'grub-bios' 'grub-efi')
provides=('grub-common' 'grub-bios' 'grub-efi')
replaces=('grub-common' 'grub-bios' 'grub-efi')
depends=('sh' 'xz' 'gettext' 'device-mapper')
makedepends=('git' 'rsync' 'xz' 'freetype2' 'ttf-dejavu' 'python3' 'autogen'
             'texinfo' 'help2man' 'gettext' 'device-mapper' 'fuse' 'bdf-unifont')
optdepends=('freetype2: For grub-mkfont usage'
            'fuse: For grub-mount usage'
            'dosfstools: For grub-mkrescue FAT FS and EFI support' 
            'efibootmgr: For grub-install EFI support'
            'libisoburn: Provides xorriso for generating grub rescue iso using grub-mkrescue'
            'os-prober: To detect other OSes when generating grub.cfg in BIOS systems'
            'mtools: For grub-mkrescue FAT FS support')
# http://git.savannah.gnu.org/cgit/grub.git
source=("https://git.savannah.nongnu.org/cgit/grub.git/snapshot/grub-${_commit}.tar.gz"
        "grub-extras::git+git://git.sv.gnu.org/grub-extras.git#branch=master"
        'grub-10_linux-detect-initramfs.patch'
        'grub.default'
        'grub.cfg'
        'sparse-inodes.patch')
md5sums=('7af5792d4684d0c104763bed8b6f83c8'
         'SKIP'
         '945527e0de8d384166a4cf23439ae9ee'
         '2f372ff858b5ad59b8b7d34dee56cd67'
         '1c03f9a8196beee4321e89d87862c69b'
         'b1583f07348e009e2d76956b8aca1121')

prepare() {
	
	cd "${srcdir}/grub-${_commit}/"
	
	## Apply fix for mkinitcpio 0.15 rw changes
	patch -p1 -i "${srcdir}/grub-10_linux-detect-initramfs.patch"
	
	## Fix DejaVuSans.ttf location so that grub-mkfont can create *.pf2 files for starfield theme
	sed 's|/usr/share/fonts/dejavu|/usr/share/fonts/dejavu /usr/share/fonts/TTF|g' -i "${srcdir}/grub-${_commit}/configure.ac"
	
	## Fix mkinitcpio 'rw' 
	sed 's| ro | rw |g' -i "${srcdir}/grub-${_commit}/util/grub.d/10_linux.in"
	
	## Fix OS naming
	sed 's|GNU/Linux|Linux|' -i "${srcdir}/grub-${_commit}/util/grub.d/10_linux.in"
	
	## Pull in latest language files
	./linguas.sh
	
	## Remove not working langs which need LC_ALL=C.UTF-8
	sed -e 's#en@cyrillic en@greek##g' -i "${srcdir}/grub-${_commit}/po/LINGUAS"
	
	## Make grub accept XFS filesystem with sparse inodes
	patch -p1 -i "${srcdir}/sparse-inodes.patch"
}

_build_grub-common_and_bios() {
	
	## Copy the source for building the bios part
	cp -r "${srcdir}/grub-${_commit}" "${srcdir}/grub-bios"
	cd "${srcdir}/grub-bios/"
	
	## Add the grub-extra sources for bios build
	install -d "${srcdir}/grub-bios/grub-extras"
	export GRUB_CONTRIB="${srcdir}/grub-bios/grub-extras/"
	
	## Unset all compiler FLAGS for bios build
	unset CFLAGS
	unset CPPFLAGS
	unset CXXFLAGS
	unset LDFLAGS
	unset MAKEFLAGS
	
	cd "${srcdir}/grub-bios/"
	
	## Run autogen.sh for bios build
	./autogen.sh
	
	## Run ./configure for bios build
	./configure \
		--with-platform="pc" \
		--target="i386" \
		--enable-efiemu \
		--enable-mm-debug \
		--enable-nls \
		--enable-device-mapper \
		--enable-cache-stats \
		--enable-boot-time \
		--enable-grub-mkfont \
		--enable-grub-mount \
		--prefix="/usr" \
		--bindir="/usr/bin" \
		--sbindir="/usr/sbin" \
		--mandir="/usr/share/man" \
		--infodir="/usr/share/info" \
		--datarootdir="/usr/share" \
		--sysconfdir="/etc" \
	 	--program-prefix="" \
		--with-bootdir="/boot" \
		--with-grubdir="grub" \
		--disable-silent-rules \
		--disable-werror
	
	## Run make for bios build
	make
}

_build_grub-efi() {
	
	## Copy the source for building the efi part"
	cp -r "${srcdir}/grub-${_commit}" "${srcdir}/grub-efi-x86_64"
	cd "${srcdir}/grub-efi-x86_64/"
	
	## Unset all compiler FLAGS for x86_64 efi build
	unset CFLAGS
	unset CPPFLAGS
	unset CXXFLAGS
	unset LDFLAGS
	unset MAKEFLAGS
	
	cd "${srcdir}/grub-efi-x86_64/"
	
	## Run autogen.sh for efi build
	./autogen.sh
	
	## Run ./configure for efi build
	./configure \
		--with-platform="efi" \
		--target="x86_64" \
		--disable-efiemu \
		--enable-mm-debug \
		--enable-nls \
		--enable-device-mapper \
		--enable-cache-stats \
		--enable-boot-time \
		--enable-grub-mkfont \
		--enable-grub-mount \
		--prefix="/usr" \
		--bindir="/usr/bin" \
		--sbindir="/usr/sbin" \
		--mandir="/usr/share/man" \
		--infodir="/usr/share/info" \
		--datarootdir="/usr/share" \
		--sysconfdir="/etc" \
		--program-prefix="" \
		--with-bootdir="/boot" \
		--with-grubdir="grub" \
		--disable-silent-rules \
		--disable-werror
	
	## Run make for efi build
	make
	
}

build() {
	
	cd "${srcdir}/grub-${_commit}/"
	
	## Build grub bios
	_build_grub-common_and_bios
	
	## Build grub efi"
	_build_grub-efi
}

_package_grub-common_and_bios() {
	
	cd "${srcdir}/grub-bios/"
	
	## Run make install for bios build
	make DESTDIR="${pkgdir}/" bashcompletiondir="/usr/share/bash-completion/completions" install
	echo
	
	## Remove gdb debugging related files for bios build
	rm -f "${pkgdir}/usr/lib/grub/i386-pc"/*.module || true
	rm -f "${pkgdir}/usr/lib/grub/i386-pc"/*.image || true
	rm -f "${pkgdir}/usr/lib/grub/i386-pc"/{kernel.exec,gdb_grub,gmodule.pl} || true
	
	## Install /etc/default/grub (used by grub-mkconfig)"
	install -D -m0644 "${srcdir}/grub.default" "${pkgdir}/etc/default/grub"
	
	## Install grub.cfg for backup array
	install -D -m0644 "${srcdir}/grub.cfg" "${pkgdir}/boot/grub/grub.cfg"
	
}

_package_grub-efi() {
	
	cd "${srcdir}/grub-efi-x86_64/"
	
	## Run make install for efi build
	make DESTDIR="${pkgdir}/" bashcompletiondir="/usr/share/bash-completion/completions" install
	
	## Remove gdb debugging related files for efi build
	rm -f "${pkgdir}/usr/lib/grub/x86_64-efi"/*.module || true
	rm -f "${pkgdir}/usr/lib/grub/x86_64-efi"/*.image || true
	rm -f "${pkgdir}/usr/lib/grub/x86_64-efi"/{kernel.exec,gdb_grub,gmodule.pl} || true
	
}

package() {
	
	cd "${srcdir}/grub-${_commit}/"
	
	## Package grub x86_64 efi 
	_package_grub-efi
	
	## Package grub bios 
	_package_grub-common_and_bios
	
}
